#!/bin/sh

#
# Test the creation and application of a trivial firmware file
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

# 1KB of all ones
TESTFILE=1K.bin

cat >"$CONFIG" <<EOF
define(TEST_DEFINE, "$TESTFILE")

# Test substitution in a field
meta-product = \${TEST_DEFINE}

# Test substitution in a resource name
file-resource "\${TEST_DEFINE}" {
        # Test substitution in the middle of a string
        host-path = "$TESTS_SRC_DIR/\${TEST_DEFINE}"
}

task complete {
        # Test substitution in a resource and no quotes
	on-resource \${TEST_DEFINE} { raw_write(0) }
}
EOF

cat >"$EXPECTED_META_CONF" <<EOF
meta-product="$TESTFILE"
file-resource "$TESTFILE" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
task "complete" {
  on-resource "$TESTFILE" {
    funlist = {"2", "raw_write", "0"}
  }
}
EOF

$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Check that the zip file was created as expected
check_meta_conf
cmp $TESTFILE_1K $UNZIPDIR/data/$TESTFILE

# Check that applying the firmware does the expected thing
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete
cmp_bytes 1024 $IMGFILE $TESTFILE_1K

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
